package com.scy.digest;

import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.security.MessageDigest;

/**
 * DigestDemo1
 *
 * @Author: 马伟奇
 * @CreateTime: 2020-05-05
 * @Description: 消息摘要算法，为了防止篡改
 * 常见的加密算法：md5，sha1 ，sha256，sha512
 */
public class DigestDemo {
    public static void main(String[] args) throws Exception {

        String plainText = "abc";
        String fileName = "apache-tomcat-9.0.95-windows-x64.zip";

        String md5Digest = getDigest4String(plainText, "MD5");
        System.out.println("md5Digest  " + md5Digest);

        String sha1Digest = getDigest4String(plainText, "SHA-1");
        System.out.println("sha1Digest  " + sha1Digest);

        String sha256Digest = getDigest4String(plainText, "SHA-256");
        System.out.println("sha256Digest  " + sha256Digest);

//        String sha256Digest2 = getDigest4String("你好abc123", "SHA-256");
//        System.out.println("sha256Digest2  " + sha256Digest2);

        String sha512Digest = getDigest4String(plainText, "SHA-512");
        System.out.println("sha512Digest  " + sha512Digest);

        String sha1File = getDigest4File(fileName, "SHA-1");
        System.out.println("sha1File  " + sha1File);
//https://downloads.apache.org/tomcat/tomcat-9/v9.0.95/bin/apache-tomcat-9.0.95-windows-x64.zip.sha512
// e4b370901526aeebd486ada45ee381a81ed71e32d942b2f8f327dd4f9922f0a0d6761d1d5c19a83bc9159046ed3b8f02e85750735df84147641445ddb59e4493 *apache-tomcat-9.0.95-windows-x64.zip
// 经验证:只更改文件名,不更改文件内容,同一种散列算法得到的散列值是一样的
        String sha512File = getDigest4File(fileName, "SHA-512");
        System.out.println(sha512File);
    }

    /**
     * 获取文件的数字摘要
     *
     * @param filePath  表示文件的路径
     * @param algorithm 表示算法
     * @return
     */
    public static String getDigest4File(String filePath, String algorithm) throws Exception {
        // 通过io流的方式读取文件
        FileInputStream fis = new FileInputStream(filePath);
        // 定义长度
        int len = -2;
        // 定义一个字节数组
        byte[] buffer = new byte[1024];
        // 边读边写
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        while (((len = fis.read(buffer)) != -1)) {
            baos.write(buffer, 0, len);
            baos.flush();
        }
        // 创建消息摘要对象
        MessageDigest digest = MessageDigest.getInstance(algorithm);
        // 执行消息摘要算法
        byte[] digestedByteArray = digest.digest(baos.toByteArray());

        fis.close();
        baos.close();

        return toHexStringForm(digestedByteArray);
    }

    /**
     * 获取数字摘要
     *
     * @param plainText     原文
     * @param algorithm 算法
     * @return
     * @throws Exception
     */
    public static String getDigest4String(String plainText, String algorithm) throws Exception {
        // 创建消息摘要对象
        MessageDigest digest = MessageDigest.getInstance(algorithm);
        // 执行消息摘要算法
        byte[] digestedByteArray = digest.digest(plainText.getBytes());

        return toHexStringForm(digestedByteArray);
    }

    public static String toHexStringForm(byte[] digest) {
        StringBuilder sb = new StringBuilder();
        // 对密文进行迭代
        for (byte b : digest) {
            // 把密文转换成16进制
            String s = Integer.toHexString(b & 0xff);
            // 判断如果密文的长度是1，需要在高位进行补0
            if (s.length() == 1) {
                s = "0" + s;
            }
            sb.append(s);
        }

        return sb.toString();
    }
}